package com.qtcm.nas.cxm.yuangong.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qtcm.nas.cxm.yuangong.pojo.Employee;
import com.qtcm.nas.cxm.yuangong.pojo.YeJi;
import com.qtcm.nas.cxm.yuangong.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/add")
    public String add(Employee employee) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        employee.setZ_date(sdf.format(new Date()));
        employeeService.addEmployee(employee);
        return "redirect:findAllPage";
    }
    @RequestMapping("/findAllPage")
    public String findAllPage(Model model, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> allPage = employeeService.findAllPage(pageNum, pageSize);
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(allPage);
        model.addAttribute("pageInfo",pageInfo);
        return "staff_list";
    }
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<Employee> all = employeeService.findAll();
        model.addAttribute("list",all);
        return "staff_List_All";
    }
    @RequestMapping("findZz")
    public String findZz(Model model){
        List<Employee> zz = employeeService.findZz();
        model.addAttribute("list",zz);
        return "staff_List_Zz";
    }
    @RequestMapping("findLz")
    public String findLz(Model model){
        List<Employee> lz = employeeService.findLz();
        model.addAttribute("list",lz);
        return "staff_List_Lz";
    }
    @RequestMapping("findById")
    public String findById(Model model,int eid){
        Employee byId = employeeService.findById(eid);
        model.addAttribute("em",byId);
        return "update_Staff";
    }

    @RequestMapping("/update")
    public String update(Employee employee){
        System.out.println(employee.toString());
        employeeService.updateEmployee(employee);
        return "forward:findAllPage";
    }
    @RequestMapping("findByYj")
    public String findByYj(Model model,HttpSession session, int eid,String firstDate,String lastDate)  {
        Employee byId = employeeService.findById(eid);
        List<YeJi> orders = employeeService.findOrders(firstDate, lastDate);
        System.out.println(orders);
        session.setAttribute("orders",orders);
        System.out.println(byId.toString());
        session.setAttribute("em",byId);
        session.setAttribute("eid",eid);
        return "staff_Achievement";
    }

}
